<template>
    <el-container>
        <el-header style="margin-top: 20px;">
            <h2>数据预处理代码展示</h2>
        </el-header>
        <el-main style="margin-top: -20px;">
            <el-card shadow="always" class="code-card">
                <el-scrollbar>
                    <pre class="code-block">
                        def process():
    # 数据库连接
    conn, cursor = connect_to_database()
    cursor.execute("SELECT * FROM movies")
    movie_data = cursor.fetchall()
    cursor.close()
    conn.close()

    logging.error("数据为:", movie_data)

    # 数据解码（假设数据库返回的数据需要解码）
    # 这里直接将元组处理为列表，不再调用 decode('utf-8')
    # 假设 movie_data 是 [(id, title, url, img_link, star, quote, number, year, people), ...]
    decoded_movie_data = [
        [value if isinstance(value, str) else str(value) for value in row]
        for row in movie_data
    ]

    # 数据清理函数
    def clean_movie_data(data):
        cleaned_data = []
        for item in data:
            # 提取和清理年份和地区
            year_info = re.sub(r'\xa0', ' ', item[7]).strip()
            people_info = re.sub(r'\xa0', ' ', item[8]).strip()

            # 提取年份和地区
            year_match = re.search(r'(\d{4})', year_info)
            year = year_match.group(1) if year_match else None
            regions = re.split(r'/', year_info)[1].strip() if '/' in year_info else None

            # 提取导演和主演
            director_match = re.search(r'导演: (.+?) 主演', people_info)
            director = director_match.group(1).strip() if director_match else None
            actors = re.split(r'主演: ', people_info)[-1].strip() if '主演' in people_info else None

            # 清理评分人数
            number = re.sub(r'[^\d]', '', item[6])

            # 整理后的数据
            cleaned_data.append({
                "id": item[0],
                "title": item[1].strip(),
                "url": item[2],
                "img_link": item[3],
                "star": float(item[4]),
                "quote": item[5].strip(),
                "number": int(number),
                "year": year,
                "regions": regions,
                "director": director,
                "actors": actors
            })
        return cleaned_data

    # 对电影数据进行清理
    cleaned_data = clean_movie_data(decoded_movie_data)

    # 写入日志
    logging.debug(f"Cleaned Movie Data: {cleaned_data}")
    save_cleaned_data(cleaned_data)

    # 返回 JSON 响应
    response = jsonify({"status": "success"})
    response.status_code = 200
    return response
            </pre>
                </el-scrollbar>
            </el-card>
        </el-main>
        <el-footer>
            <el-alert title="提示：请确保运行环境正确配置。" type="info" show-icon>
            </el-alert>
        </el-footer>
    </el-container>
</template>

<script>
    export default {
        methods: {
            runSpider() {
                console.log("爬虫任务开始...");
                // 在这里调用后端 API 启动爬虫任务
            },
            clearLogs() {
                console.log("日志已清除");
                // 实现清除日志功能
            },
        },
    };
</script>

<style scoped>
    /* .code-card { */
        /* margin: 20px 0; */
        /* padding: 10px; */
        /* background-color: #f5f5f5; */
        /* border-radius: 8px; */
    /* } */

    /* .code-block { */
        /* font-family: "Courier New", Courier, monospace; */
        /* font-size: 14px; */
        /* color: #333; */
        /* white-space: pre-wrap; */
        /* line-height: 1.5; */
    /* } */

    .button-group {
        display: flex;
        justify-content: space-between;
        margin-top: 0px;
    }
</style>